package org.ggn.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.ggn.dao.CourseDao;
import org.ggn.model.Course;
import org.ggn.util.DirFinalValue;
import org.junit.Test;

public class test {

	@Test
	public void test() {
		/*Session session = null;
		Grade u = null;
		try {
			session = HibernateUtil.openSession();
			u = (Grade)session.load(Grade.class, 1);
			System.out.println(u.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.close(session);
		}
		GradeDao gd = new GradeDao();
		Grade g = gd.load(1);
		System.out.println(g.getName());
		System.out.println(g.getCourses().size());*/
		/**
		 * 此时session已经关闭，取不到size。。。
		 */
	}
	@Test
	/**
	 * 测试文件上传
	 */
	public void test01(){
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			CourseDao cd = new CourseDao();
			Course c = cd.load(8);
			String filepath = "I:\\学习\\第二学期\\高教版高数下习题详解\\高教版高数下册答案\\第7章 习题详细解答.doc";
			String filename = filepath.substring(filepath.lastIndexOf("\\"));
			bis = new BufferedInputStream(new FileInputStream(filepath));
			bos = new BufferedOutputStream(new FileOutputStream(DirFinalValue.rootpath+"\\"+c.getGrade().getName()+"\\"+c.getShortname()+DirFinalValue.exam_iofo+filename));
			int len = 0;
			byte[] buff = new byte[1024];
			
			try {
				while((len = bis.read(buff))>=0){
					bos.write(buff, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			try {
				if(bis!=null)bis.close();
				if(bos!=null)bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	@Test
	/**
	 * 测试文件下载
	 */
	public void test02(){
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			String filepath = "I:\\学习\\第二学期\\高教版高数下习题详解\\高教版高数下册答案";
			File file = new File(filepath);
			String [] files = file.list();
			for(String s :files){
				System.out.println(s);
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		
	}

}
